package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoTimeZone;
import com.ocom.accesservice.service.TimeZoneService;
import com.ocom.common.entity.access.EntityMjTimeZone;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TimeZoneServiceImpl extends ServiceImpl<IDaoTimeZone, EntityMjTimeZone> implements TimeZoneService {


    @Autowired
    private IDaoTimeZone daoTimeZone;




    /**
     * 初始化单位门禁参数
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    @Transactional
    @Override
    public void InitCompanyPara(EntityMjTimeZone entityMjTimeZone,Long companyId) {
        daoTimeZone.insert(entityMjTimeZone);
    }

    /*
     * @Description: 获取时区列表
     * @Author: lu 
     * @Date: 2022/1/13 10:28
     * @param companyId:单位编号
     * @return: com.ocom.common.resp.Result
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public List<Map<String,Object>> selectGetZoneList(Long companyId) {

        QueryWrapper<EntityMjTimeZone> wrapper = new QueryWrapper<>();
        wrapper.select("zone_no","zone_name");
        wrapper.eq("com_id",companyId);
        wrapper.eq("status",0);
        wrapper.orderByAsc("zone_no");
        List<EntityMjTimeZone> list=daoTimeZone.selectList(wrapper);
        List<Map<String,Object>> retList=new ArrayList<>();
        if (list.size()>0){
            for (EntityMjTimeZone entityMjTimeZone : list) {
                Map<String,Object> map=new HashMap<>();
                map.put("zoneNo",entityMjTimeZone.getZoneNo());
                map.put("zoneName",entityMjTimeZone.getZoneName());
                retList.add(map);
            }
           return retList;
        }else {
            return null;
        }
    }

    /*
     * @Description: 获取时区设置列表,按zoneNo查询zoneName
     * @Author: lu 
     * @Date: 2022/1/13 16:29
     * @param zoneNo: 
     * @param comId:  
     * @return: com.ocom.common.entity.access.EntityMjTimeZone
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB)
    @Override
    public EntityMjTimeZone selectZoneName(long zoneNo, long companyId) {
        QueryWrapper<EntityMjTimeZone> wrapper = new QueryWrapper<>();
        wrapper.select("zone_name");
        wrapper.eq("com_id",companyId);
        wrapper.eq("zone_no",zoneNo);
        wrapper.orderByDesc("id");
        wrapper.last("limit 1");
        return daoTimeZone.selectOne(wrapper);
    }
}
